#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "Comm.hpp"


void Usage(const std::string &process)
{
    std::cout << "Usage: " << process << " server_ip server_port" << std::endl;
}

// ./tcp_client serverip serverport
int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        Usage(argv[0]);
        return 1;
    }

    std::string serverip=argv[1];
    uint16_t serverport=std::stoi(argv[2]);


    int sockfd=socket(AF_INET,SOCK_STREAM,0);
    if(sockfd < 0)
    {
        std::cerr << "socket error" << std::endl;
        return 1;
    }
    //必须要有Ip和Port, 需要bind，但是不需要用户显示的bind，client系统随机端口

    struct sockaddr_in server;
    memset(&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port=htons(serverport);
    inet_pton(AF_INET,serverip.c_str(),&server.sin_addr);

    int n=connect(sockfd,CONV(&server),sizeof(server));
    if(n < 0)
    {
        std::cerr << "connect error" << std::endl;
        return 2;
    }

    while(true)
    {
        std::string inbuffer;
        std::cout << "Please Enter# ";
        std::getline(std::cin, inbuffer);
        ssize_t n=write(sockfd,inbuffer.c_str(),inbuffer.size());
        if(n>0)
        {
            char buffer[1024];
            ssize_t m=read(sockfd,buffer,sizeof(buffer)-1);
            if(m>0)
            {
                buffer[m]=0;
                std::cout << "get a echo messsge -> " << buffer << std::endl;
            }
            else if(m==0||m<0)
            {
                break;
            }
        }
        else
        {
            break;
        }
    }

    close(sockfd);
    return 0;


}